﻿using AutoMapper;
using MediatR;
using WMS.API.Application.Command.InventoryManagement;
using WMS.Domain.Entities.InventoryManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.InventoryManagement
{
    /// <summary>
    /// 库存管理-箱子型号管理修改处理类-grx
    /// </summary>
    public class UpdateBoxCommandHandler : IRequestHandler<UpdateBoxCommand, APIResult<int>>
    {
        private readonly IBaseRepository<BoxManagement> _boxRepository;
        private readonly IMapper mapper;
        public UpdateBoxCommandHandler(IBaseRepository<BoxManagement> boxRepository, IMapper mapper)
        {
            _boxRepository = boxRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 修改箱子型号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(UpdateBoxCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res=new APIResult<int>("");
            
            var box =await _boxRepository.FindAsync(request.Id);
            if (box == null)
            {
                res.Code = ResultCode.Failed;
                res.Message = "箱子型号不存在";
                res.Data = 0;
                return await Task.FromResult(res);
            }
            var model = mapper.Map(request, box);
            var i=  await  _boxRepository.UpdateAsync(model);
            res.Code =i>0? ResultCode.Success:ResultCode.Failed;
            res.Message = i > 0 ? "修改箱子型号成功" : "修改箱子型号失败";
            return await Task.FromResult(res);
        }
    }
}
